package per.kongtouxu.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import per.kongtouxu.domain.dto.JsonDto;
import per.kongtouxu.domain.pojo.Admin;
import per.kongtouxu.domain.pojo.FileInfo;
import per.kongtouxu.domain.pojo.FileType;
import per.kongtouxu.domain.pojo.PageBean;
import per.kongtouxu.domain.vo.V_FileInfo;
import per.kongtouxu.service.FileInfoService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.UUID;

@Controller
@RequestMapping("/fileInfo")
public class FileInfoController {
    @Autowired
    private FileInfoService fileInfoService;
    //分页查询
    @RequestMapping("/pageFind")
    public @ResponseBody PageBean<V_FileInfo> pageFind(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数
        String fileName = req.getParameter("FileName");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        String fileState = req.getParameter("fileState");
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        //2.查询service
        PageBean<V_FileInfo> pageBean = this.fileInfoService.pageFind(fileName, startTime, endTime,Integer.parseInt(fileState), Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        System.out.println(pageBean);
        //3.返回JSON
        return pageBean;
    }
    //审核操作
    @RequestMapping("/check")
    public @ResponseBody JsonDto fileCheck(HttpServletRequest request,Integer fileStateId,Integer fileId){
        //1.获取登录用户
        Admin loginAdmin =(Admin) request.getSession().getAttribute("loginAdmin");
        Long adminId = loginAdmin.getAdminId();
        Integer LoginId = Math.toIntExact(adminId);
        //2.调用serivce
        fileInfoService.updateCheck(fileStateId,fileId,LoginId);

        JsonDto jsonDto = new JsonDto();
        jsonDto.setMessage("审核成功");
        return jsonDto;
    }
    //文件下载
    @RequestMapping("/download")
    public void download( HttpServletResponse response,String filename,String path) throws IOException {
        System.out.println("filename:"+filename);
        System.out.println("path:"+path);
        // 设置下载的文件的文件名
        response.addHeader("Content-Disposition", "attachment;filename="
                + URLEncoder.encode(filename,"UTF-8"));
        //1.设置文件ContentType类型，这样设置，会自动判断下载文件类型
        response.setContentType("multipart/form-data");
        // 将文件发送出去
        FileUtils.copyFile(new File(path), response.getOutputStream());
    }
}
